<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch">
      <el-form-item label="关键字" prop="search">
        <el-input v-model="queryParams.search" placeholder="UID、昵称、电话号码" clearable style="width: 240px" @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="出金审批" prop="status">
        <el-select v-model="queryParams.status" placeholder="出金审批" clearable style="width: 240px">
          <el-option v-for="dict in statusList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="流水类型" prop="typeCode">
        <el-select v-model="queryParams.typeCode" placeholder="流水类型" clearable style="width: 240px">
          <el-option v-for="dict in typeCodeList" :key="dict.value" :label="dict.label" :value="dict.value" />
        </el-select>
      </el-form-item>
      <el-form-item label="创建时间">
        <el-date-picker v-model="dateRange" style="width: 340px" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期" :default-time="['00:00:00', '23:59:59']" value-format="yyyy-MM-dd HH:mm:ss"></el-date-picker>

        <!-- <el-date-picker v-model="dateRange" style="width: 240px" value-format="yyyy-MM-dd" type="datetimerange" range-separator="-" start-placeholder="开始日期" end-placeholder="结束日期"></el-date-picker> -->
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-table :height="tableHeight" show-summary @sort-change="changeTableSort" :summary-method="getSummaries" v-loading="loading" :data="list" stripe border>
      <el-table-column label="序号" type="index" width="60" align="center" />
      <el-table-column label="创建时间" prop="createTime" align="center" width="180" :sortable="'custom'" />
      <el-table-column label="UID" prop="userId" width="80" align="center" :sortable="'custom'" />
      <el-table-column label="昵称" prop="userNickName" width="120" align="center" />
      <el-table-column label="用户手机" prop="phone" width="120" align="center" />
      <el-table-column label="进账(元)" prop="inMoney" width="150" align="center" :sortable="'custom'" />
      <el-table-column label="出账(元)" prop="outMoney" width="150" align="center" :sortable="'custom'" />
      <el-table-column label="手续费(元)" prop="withdrawCharge" width="150" align="center" :sortable="'custom'" />
      <el-table-column label="实付金额(元)" prop="actualAmount" width="150" align="center" :sortable="'custom'" />
      <el-table-column label="流水类型" prop="typeCode" width="120" align="center" :sortable="'custom'">
        <template slot-scope="scope">
          <span> {{selectDictLabel(typeCode,scope.row.typeCode) }}</span>
        </template>
      </el-table-column>
      <el-table-column label="说明" prop="remark" width="250" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" fixed="right">
        <template slot-scope="scope">
          <el-button size="mini" type="text" icon="" @click="handleUpdate(scope.row)">查看</el-button>
        </template>
      </el-table-column>
    </el-table>

    <pagination v-show="total > 0" :total="total" :page.sync="queryParams.pageNum" :limit.sync="queryParams.pageSize" @pagination="getList" />

    <!-- 会员账单详情 -->
    <z-dialog :title="title" :visible.sync="open" :isShowConfirm="false">
      <form-view :form="form"></form-view>
    </z-dialog>
  </div>
</template>

<script>
import { listAppLogCash } from "@/api/publicBill/member.js";
import formView from './formView.vue'
export default {
  name: "MemberBill",
  components: { formView },
  data() {
    return {

      // 遮罩层
      loading: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      list: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,

      // 日期范围
      dateRange: [],

      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: this.global.pageSize,
      },
      pageNum: 1,

      // 表单参数
      form: {},

      //出金审批（0待审核,1审核通过,2退回用户,3审核拒绝,4出金中,5出金完成,6出金失败）
      status: [
        { label: '待审核', value: 0 },
        { label: '审核通过', value: 1 },
        { label: '退回用户', value: 2 },
        { label: '审核拒绝', value: 3 },
        { label: '出金中', value: 4 },
        { label: '出金完成', value: 5 },
        { label: '出金失败', value: 6 },
      ],

      // 流水类型（10现金充值、11现金提现、12售出入账、13转售失败收益、14提现退回、20佣金获得、21佣金转现、30保证金、
      //31余款支付，40转售手续费，41确权费，50购买席位，51席位收益，60购买消费品，61兑换商品、
      //62兑换商品手续费、70金会员服务收益、80管理端操作现金、81管理端操作佣金）
      typeCode: [
        { label: '现金充值', value: 10 },
        { label: '成功出金', value: 11 },
        { label: '售出入账', value: 12 },
        { label: '未结款补偿', value: 13 },
        { label: '提现冻结', value: 14 },
        { label: '提现拒绝退回', value: 15 },
        { label: '提现银行失败', value: 16 },
        { label: '退手续费', value: 17 },

        // { label: '佣金获得', value: 20 },
        // { label: '佣金转现', value: 21 },
        { label: '保证金', value: 30 },
        { label: '支付商品', value: 31 },
        { label: '开馆之礼出价', value: 32 },


        { label: '开馆之礼保证金', value: 33 },
        { label: '开馆之礼支付商品', value: 34 },
        { label: '保证金', value: 35 },

        { label: '转售手续费', value: 40 },
        // { label: '确权费', value: 41 },
        { label: '购买席位', value: 50 },
        // { label: '席位收益', value: 51 },
        { label: '购买消费品', value: 60 },
        { label: '兑换商品', value: 61 },
        { label: '兑换商品手续费', value: 62 },
        // { label: '金会员服务收益', value: 70 },
        { label: '佣金转入', value: 71 },

        { label: '管理端操作', value: 80 },
        // { label: '管理端操作佣金', value: 81 },
      ]
    };
  },
  watch: {
    "queryParams": {
      deep: true,
      handler: function (newV, oldV) {

        if (this.pageNum == newV.pageNum) {
          this.handleQuery();
        } else {
          this.pageNum = newV.pageNum;
        }
      },
    },
    dateRange(newV, oldV) {
      if (newV) {
        if (newV.length >= 2) {
          this.queryParams.createTimeStart = String(newV[0]);
          this.queryParams.createTimeEnd = String(newV[1]);
        }
      } else {
        this.queryParams.createTimeStart = undefined;
        this.queryParams.createTimeEnd = undefined;
      }
      this.getList();
    },
  },
  computed: {
    tableHeight() {
      return 'calc(100vh - 280px)'
    },

    statusList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.status));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },

    typeCodeList() {
      let typeDictArray = JSON.parse(JSON.stringify(this.typeCode));
      typeDictArray.unshift({ label: "全部", value: undefined });
      return typeDictArray;
    },
  },

  created() {
    this.getList();
  },
  methods: {
    getList() {
      this.loading = true;
      listAppLogCash(this.queryParams).then((response) => {
        this.list = response.data;
        this.total = response.total;
        this.loading = false;
      });
    },
    changeTableSort(column) {
      this.queryParams.orderBy = column.prop;
      this.queryParams.isAsc = (column.order == 'descending' ? 0 : 1); //ascending == 升序 descending == 降序  0 倒,1正
      this.getList();
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },

    // 表单重置
    reset() {

      this.resetForm("form");
    },

    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.dateRange = [];
      this.resetForm("queryForm");
      this.queryParams.createTimeStart = undefined;
      this.queryParams.createTimeEnd = undefined;
      this.dateRange = undefined;
      this.handleQuery();
    },

    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.open = true;
      this.title = "会员账单";
      this.form = row;
    },

    /** 提交按钮 */
    submitForm: function () {
      // console.log(this.$refs.childMethod.currentValue);
      // this.$refs["form"].validate((valid) => {});
    },

    getSummaries(param) {
      const { columns, data } = param;
      const sums = [];
      columns.forEach((column, index) => {
        if (index === 0) {
          sums[index] = '合计';
          return;
        }
        if (index === 1 || index === 2 || index === 3 || index === 4 || index === 9 || index === 10 || index === 11) {
          // sums[index] = '';
          return;
        }
        const values = data.map(item => Number(item[column.property]));
        if (!values.every(value => isNaN(value))) {
          sums[index] = values.reduce((prev, curr) => {
            const value = Number(curr);

            if (!isNaN(value)) {
              return prev + curr;
            } else {
              return prev;
            }
          }, 0);

          var num = sums[index];
          num = num.toFixed(2)
          sums[index] = num;
          sums[index] += ' 元';
        } else {
          sums[index] = 'N/A';
        }
      });

      return sums;
    }

  },
};
</script>
<style scoped>
</style>
